<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="size" slot-scope="{row}">
        {{`${(row.size/1024/1024).toFixed(2)} MB`}}
      </template>
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   plain
                   v-if="permission.file_upload"
                   icon="el-icon-upload2"
                   @click="handleUpload">上 传
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text"
                   icon="el-icon-download"
                   size="small"
                   v-if="permission.file_download"
                   @click="handleDownload(scope.row)">下载
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="上传文件（<100M）"
               append-to-body
               :visible.sync="attachBox"
               width="555px">
      <avue-form ref="form" :option="attachOption" v-model="attachForm" :upload-after="uploadAfter">
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, remove,} from "@/api/desk/file";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {
          'str': '',
        },
        loading: true,
        selectionList: [],
        option: {
          defaultSort:{
            prop: 'modified',
            order: 'descending'
          },
          lazy: true,
          height: "auto",
          calcHeight: 230,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          addBtn: false,
          addRowBtn: false,
          cellBtn: true,
          // selection: true,
          dialogDrag: true,
          column: [
            {
              label: "文件名",
              prop: "name",
              search: true,
              searchOrder: 1,
            },
            {
              label: "路径",
              prop: "path",
            },
            {
              label: "文件大小",
              prop: "size",
            },
            {
              label: "修改时间",
              prop: "modified",
            },
          ]
        },
        data: [],
        attachBox: false,
        attachForm: {},
        attachOption: {
          submitBtn: false,
          emptyBtn: false,
          column: [
            {
              // label: '（<100M）',
              prop: 'attachFile',
              type: 'upload',
              drag: true,
              loadText: '模板上传中，请稍等',
              span: 24,
              propsHttp: {
                res: 'data'
              },
              action: "/api/chfs/upload"
            }
          ]
        }
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(false),
          viewBtn: this.vaildData(false),
          delBtn: this.vaildData(this.permission.file_delete, false),
          editBtn: this.vaildData(false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
    },
    methods: {
      handleDownload(row) {
        // window.open(location.protocol + '//' + location.hostname + `${row.jmxUrl}`);
        window.open(`/api/chfs/shared` + `${row.path}`);
      },
      handleUpload() {
        this.attachBox = true;
      },
      uploadAfter(res, done, loading, column) {
        window.console.log(column);
        this.attachBox = false;
        this.refreshChange();
        done();
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.path);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },

      searchReset() {
        this.query = {'str': '',}
        this.onLoad(this.page);
      },
      searchChange(params,done) {
        console.log(params.name)
        this.query.str=params.name;

        this.onLoad(this.query)
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(params = {}) {
        this.loading = true;
        getList(Object.assign(params, this.query)).then(res => {
          const data = res.data;
          this.data = data.files;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

